'use strict';

const Controller = require('egg').Controller;
const { Op } = require('sequelize');
const crypto = require('crypto');
class ModifypasswordController extends Controller {
    // 专门对数据进行md5加密的方法，输入明文返回密文
    getMd5Data(data) {
        return crypto.createHash('md5').update(data).digest('hex');
    }
    async update_student(){
        const { ctx } = this;
        const id = ctx.params.id;
        let { oldPassword, newPassword } = ctx.request.body;
        // 从数据库中将原来的密码
        const old_password = await ctx.model.Student.findOne({ 
            attributes: ['password'] 
        }, {
            where: { id }
        });

        //密码加密
        oldPassword = this.getMd5Data(oldPassword); 
        newPassword = this.getMd5Data(newPassword);
        console.log(oldPassword, old_password.password, newPassword);
        //密码对比
        if (oldPassword == old_password.password) {
            const result = await ctx.model.Student.update({
                password:newPassword
            }, {
                where: { id }
            });
            if (result[0] == 1) {
                ctx.body = {
                    code: 1,
                    msg: '密码修改成功'
                }
            } else {
                ctx.body = {
                    code: 0,
                    msg: '密码修改失败'
                }
            }
        } else {
            ctx.body = {
                code: 2,
                msg: '请正确输入原始密码'
            }
        }
        ctx.status = 200;
    }
    async update_teacher(){
        const { ctx } = this;
        const id = ctx.params.id;
        let { oldPassword, newPassword } = ctx.request.body;
        // 从数据库中将原来的密码
        const old_password = await ctx.model.Teacher.findOne({ 
            attributes: ['password'] 
        }, {
            where: { id }
        });

        //密码加密
        oldPassword = this.getMd5Data(oldPassword); 
        newPassword = this.getMd5Data(newPassword);
        console.log(oldPassword, old_password.password, newPassword);
        //密码对比
        if (oldPassword == old_password.password) {
            const result = await ctx.model.Teacher.update({
                password:newPassword
            }, {
                where: { id }
            });
            if (result[0] == 1) {
                ctx.body = {
                    code: 1,
                    msg: '密码修改成功'
                }
            } else {
                ctx.body = {
                    code: 0,
                    msg: '密码修改失败'
                }
            }
        } else {
            ctx.body = {
                code: 2,
                msg: '请正确输入原始密码'
            }
        }
        ctx.status = 200;
    }
    async update_secretary(){
        const { ctx } = this;
        const id = ctx.params.id;
        let { oldPassword, newPassword } = ctx.request.body;
        // 从数据库中将原来的密码
        const old_password = await ctx.model.Secretary.findOne({ 
            attributes: ['password'] 
        }, {
            where: { id }
        });

        //密码加密
        oldPassword = this.getMd5Data(oldPassword); 
        newPassword = this.getMd5Data(newPassword);
        console.log(oldPassword, old_password.password, newPassword);
        //密码对比
        if (oldPassword == old_password.password) {
            const result = await ctx.model.Secretary.update({
                password:newPassword
            }, {
                where: { id }
            });
            if (result[0] == 1) {
                ctx.body = {
                    code: 1,
                    msg: '密码修改成功'
                }
            } else {
                ctx.body = {
                    code: 0,
                    msg: '密码修改失败'
                }
            }
        } else {
            ctx.body = {
                code: 2,
                msg: '请正确输入原始密码'
            }
        }
        ctx.status = 200;
    }
}
module.exports = ModifypasswordController;
